package com.nit.bike.mapper.basic;

import com.nit.bike.dto.basic.UserDTO;
import com.nit.bike.entity.basic.UserEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {
    int deleteByPrimaryKey(Long userId);

    int insert(UserEntity record);

    int insertSelective(UserEntity record);

    UserEntity selectByPrimaryKey(Long userId);

    int updateByPrimaryKeySelective(UserEntity record);

    int updateByPrimaryKey(UserEntity record);

    /**
     * 获取表格数据
     */
    @Select({"<script>SELECT *, u.user_id AS `key`, c.card_number " +
            "FROM basic_user u " +
            "LEFT JOIN basic_card c USING(card_id) " +
            "WHERE TRUE " +
            "<if test='loginName != null'>AND login_name LIKE CONCAT('%', #{loginName}, '%') </if>" +
            "<if test='userName != null'>AND user_name LIKE CONCAT('%', #{userName}, '%') </if>" +
            "<if test='roleId != null'>AND u.role_id = #{roleId} </if>" +
            "<if test='userPhone != null'>AND user_phone LIKE CONCAT('%', #{userPhone}, '%') </if>" +
            "<if test='userState != null'>AND user_state = #{userState} </if>" +
            "<if test='idCard != null'>AND id_card LIKE CONCAT('%', #{idCard}, '%') </if>" +
            "ORDER BY " +
            "<if test='sortField != null'>" +
            "${sortField} ${sortOrder}, " +
            "</if>" +
            "u.user_id DESC " +
            "</script>"})
    List<UserDTO> list4Table(
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("loginName") String loginName,
            @Param("userName") String userName,
            @Param("roleId") String roleId,
            @Param("userPhone") String userPhone,
            @Param("userState") String userState,
            @Param("idCard") String idCard,
            @Param("sortField") String sortField,
            @Param("sortOrder") String sortOrder) throws Exception;

    /**
     * 重置用户密码
     */
    @Update({"UPDATE basic_user " +
            "SET login_pwd = #{loginPwd} " +
            "WHERE user_id = #{userId} AND user_state = 0 "
    })
    Integer resetPwd(@Param("userId") Long userId,
                     @Param("loginPwd") String loginPwd) throws Exception;

    /**
     * 修改用户状态
     */
    @Update("UPDATE basic_user " +
            "SET user_state = #{userState} " +
            "WHERE user_id = #{userId}")
    Integer updateState(@Param("userId") Long userId,
                        @Param("userState") Integer userState) throws Exception;

    /**
     * 修改用户当前骑行记录
     */
    @Update("UPDATE basic_user " +
            "SET ride_id = #{rideId} " +
            "WHERE user_id = #{userId}")
    void updateRideId(@Param("userId") Long userId,
                      @Param("rideId") Long rideId) throws Exception;

    /**
     * 根据用户登录名称获取用户信息
     */
    @Select({"SELECT * " +
            "FROM basic_user " +
            "WHERE login_name = #{loginName} AND user_state != 2 LIMIT 1"
    })
    UserDTO getByLoginName(@Param("loginName") String loginName) throws Exception;

    /**
     * 根据用户ID获取用户信息
     */
    @Select("SELECT * " +
            "FROM basic_user u " +
            "LEFT JOIN basic_card c ON u.card_id = c.card_id AND u.user_id = c.user_id " +
            "WHERE u.user_id = #{userId} AND user_state = 0 " +
            "LIMIT 1"
    )
    UserDTO getByUserId(@Param("userId") Long userId) throws Exception;

    /**
     * 更新用户租借卡id
     */
    @Update("UPDATE basic_user " +
            "SET card_id = #{cardId} " +
            "WHERE user_id = #{userId} "
    )
    Integer updateCardId(@Param("userId") Long userId, @Param("cardId") Long cardId) throws Exception;


    /**
     * 根据租借卡id获取用户信息
     */
    @Select({"SELECT * " +
            "FROM basic_user " +
            "WHERE card_id = #{cardId} AND user_state = 0 " +
            "LIMIT 1"
    })
    UserDTO getByCardId(@Param("cardId") Long cardId) throws Exception;
}
